package com.yl.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yl.auth.entity.Role;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author L
 * @since 2023-07-19
 */
public interface JigouzhongxinduanroleMapper extends BaseMapper<Role> {

    @Select("select r.id,r.name,count(a.id) count,r.create_time,r.is_delete from role r\n" +
            "            left join role_account ra on r.id = ra.role_id\n" +
            "            left join jigouzhongxinduanaccount a on ra.account_id = a.id\n" +
            "            where r.is_delete = 0 && r.type = 2\n" +
            "            group by r.id\n" +
            "            order by r.id desc")
    Page<Role> findAllAndPage(Page<Role> page1);

    @Insert("insert into center_role_permission(id,role_id,permission_id) values(0,#{roleId},#{perId})")
    void savePermission(@Param("roleId") Integer roleId,@Param("perId") Long perId);

    @Select("select p.id from role r\n" +
            "left join center_role_permission rp on r.id  = rp.role_id\n" +
            "left join permission p on rp.permission_id = p.id\n" +
            "where r.id = #{id}")
    List<Long> getRoleById(@Param("id") Long id);

    @Delete("delete from center_role_permission where role_id = #{id}")
    void deletePerByRoleId(@Param("id") Long id);

    @Delete("delete from role_account where role_id = #{id}")
    void deleteRaByRoleId(@Param("id") Long id);

    @Select("select r.id id,r.name name,COUNT(u.id) count,r.create_time create_time from role r\n" +
            "left join role_account ar on r.id=ar.role_id\n" +
            "left join jigouzhongxinduanaccount u on u.id=ar.account_id\n" +
            "where r.id= #{id}\n" +
            "group by r.id")
    Role getByIdRole(@Param("id") Integer id);
}
